package com.monkeygroup.mygoplus.view.home.fragment;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.common.utils.image.Images;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.home.activity.person.PersonChooseActivity;
import com.monkeygroup.mygoplus.view.home.activity.person.PersonCollectActivity;
import com.monkeygroup.mygoplus.view.home.activity.person.PersonInformationActivity;
import com.monkeygroup.mygoplus.view.home.activity.person.PersonSettingActivity;
import com.monkeygroup.mygoplus.view.home.activity.person.PersonalPropertyManageActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/12.
 */
public class PersonalFragment extends Fragment implements View.OnClickListener {
    private View mView;
    private Activity activity;
    private final String TAG = "PersonalFragment";

    private LinearLayout ll_identify,ll_bonus,layout_shortcut;
    private LinearLayout ll_info,ll_collect,ll_manager;
    private TextView txt_name,txt_identify;

    private SimpleDraweeView sdv_2;
    private PersonalNomalInfo user;
    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        activity = getActivity();
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_personal1, container, false);
            initView();
            initListener();
        }
        return mView;
    }

    private void initView() {
//        ll_identify = (LinearLayout)mView.findViewById(R.id.ll_identify);
//        ll_bonus= (LinearLayout)mView.findViewById(R.id.ll_bonus);
        layout_shortcut= (LinearLayout)mView.findViewById(R.id.layout_shortcut);

        ll_info= (LinearLayout)mView.findViewById(R.id.ll_info);
        ll_collect= (LinearLayout)mView.findViewById(R.id.ll_collect);
        ll_manager= (LinearLayout)mView.findViewById(R.id.ll_manager);

        txt_name= (TextView)mView.findViewById(R.id.txt_name);
        txt_identify= (TextView)mView.findViewById(R.id.txt_identify);

        sdv_2= (SimpleDraweeView)mView.findViewById(R.id.sdv_2);
        user = LocalInfo.getUserInfo(activity);
        if(!StringUtil.isBlank(user.getHeadIcon())){
            sdv_2.setBackgroundDrawable(new BitmapDrawable());
            Images.with(activity).load(Uri.parse(user.getHeadIcon())).resize(130,90).into(sdv_2);
        }
        txt_name.setText(user.getName());

        params = new HashMap<>();
        params.put("mobile",user.getMobile());
        initRequest();
        httpModel.startRequest();

    }

    private void initListener() {
        mView.findViewById(R.id.layout_shortcut).setOnClickListener(this);
//        mView.findViewById(R.id.ll_identify).setOnClickListener(this);
//        mView.findViewById(R.id.btn_home_sell2).setOnClickListener(this);
//        mView.findViewById(R.id.btn_home_sell1).setOnClickListener(this);
//        mView.findViewById(R.id.btn_home_sell).setOnClickListener(this);
        mView.findViewById(R.id.ll_info).setOnClickListener(this);
        mView.findViewById(R.id.ll_collect).setOnClickListener(this);
        mView.findViewById(R.id.ll_manager).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_shortcut:
                PersonSettingActivity.gotoActivity(activity);
                break;
//            case R.id.ll_identify:
//                PersonChooseActivity.gotoActivity(activity);
//                break;
//            case R.id.btn_home_sell2:
//                PersonInformationActivity.gotoActivity(activity);
//                break;
//            case R.id.btn_home_sell1:
//                PersonCollectActivity.gotoActivity(activity);
//                break;
//            case R.id.btn_home_sell:
//                PersonalPropertyManageActivity.gotoActivity(activity);
//                break;
            case R.id.ll_info:
                PersonInformationActivity.gotoActivity(activity);
                break;
            case R.id.ll_collect:
                PersonCollectActivity.gotoActivity(activity);
                break;
            case R.id.ll_manager:
                PersonalPropertyManageActivity.gotoActivity(activity);
                break;

            default:
                break;
        }
    }

    private void initRequest() {


        httpModel = new HttpModel(RequestUrl.baseUrl + RequestUrl.userinfourl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (VilidateUtil.isSuccess(resultCode)) {
                    Log.e("String.valueOf(a)))", String.valueOf(((Map) data).get("headIcon")));
                    String headPic = String.valueOf(((Map) data).get("headIcon"));

                    sdv_2.setBackgroundDrawable(new BitmapDrawable());
                    Images.with(activity).load(Uri.parse(headPic)).resize(130, 90).into(sdv_2);
                } else {

                }

            }
        }, true);

    }

}
